package qhy.websocket.qhy_chat_websocket.config;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author qiuhongyu
 * @date 2021/1/26 9:58
 */
public class SessionPool {

    public static Map<String, Session> sessions = new ConcurrentHashMap<>();

    public static void close(String sessionId) throws IOException {
        for (String userId : SessionPool.sessions.keySet()) {
            Session session = SessionPool.sessions.get(userId);
            if (session.getId().equals(sessionId)) {
                sessions.remove(userId);
                break;

            }
        }
    }

    public static void sendMessage(String sessionId, String message) {
        sessions.get(sessionId).getAsyncRemote().sendText(message);
    }

    public static void sendMessage(String message) {
        for (String sessionId : SessionPool.sessions.keySet()) {
            SessionPool.sessions.get(sessionId).getAsyncRemote().sendText(message);
        }
    }

    public static void sendMessage(Map<String,Object> params) {
        //前端传递的是这样的json:{"fromUserId":userId,"toUserId":toUserId,"msg":msg};
        String fromUserId = params.get("fromUserId").toString();
        String toUserId = params.get("toUserId").toString();
        String msg = params.get("msg").toString();
        msg = "来自" + fromUserId + "的消息：" + msg;
        Session session = sessions.get(toUserId);
        if (session != null) {
            //点对点发送
            session.getAsyncRemote().sendText(msg);
        }
    }
}
